<?php

namespace App\Http\Controllers\Company;

use App\Models\Category;
use App\Models\CompanyProfile;
use App\Models\District;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
use App\Http\Controllers\Core\HomeController as Home;

class InfoController extends Home
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    public function security()
    {
        return view('company.info.security');
    }

    /**
     * Show the application dashboard.
     *
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        if (!$this->isLogin()) {
            return redirect('login');
        }
        $member = session::get('member');
        $company = CompanyProfile::where(['user_id' => $member->id])->get();
        if (!$company = $company->first()) {
            return "不存在该公司";
        };
        // TODO 分类信息后期做缓存
        $category = Category::category_cache();
        $tagIdArr = explode(',', $company->tag) ? explode(',', $company->tag) : [];

        $tag = Category::whereIn('id', $tagIdArr)->get()->toArray();
        $tagKeyArr = implode(",", array_column($tag, 'id'));
        $tagValueArr = implode("+", array_column($tag, 'name'));
        $data = [
            'company' => $company,
            'category' => $category,
            'tagKeyArr' => $tagKeyArr,
            'tagValueArr' => $tagValueArr
        ];
        // TODO 基本信息
        if ($request->method() == 'POST') {
            $nature = (int)$request->get('nature');
            $trade = (int)$request->get('trade');
            $scale = (int)$request->get('scale');

            $company->name = $request->get('name');
            $company->short_name = $request->get('short_name');
            $company->nature = $request->get('nature');
            $company->nature_cn = isSet($category['_company_type'][$nature]) ? $category['_company_type'][$nature] : '';
            $company->trade = $request->get('trade');
            $company->trade_cn = isSet($category['_trade'][$trade]) ? $category['_trade'][$trade] : '';
            $company->scale = $request->get('scale');
            $company->scale_cn = isSet($category['_scale'][$scale]) ? $category['_scale'][$scale] : '';
            $company->registered = $request->get('registered') ? $request->get('registered') : "";
            $company->city_cn = $request->get('city_cn');
            $company->proper_cn = $request->get('proper_cn');
            $company->area_cn = $request->get('area_cn');
            $company->city = $request->get('city');
            $company->proper = $request->get('proper');
            $company->area = $request->get('area');
            $company->district_cn = $request->get('district');
            $company->address = $request->get('address') ? $request->get('address') : "";
            $company->tag = $request->get('tag') ? $request->get('tag') : "";
            $company->contents = $request->get('contents');
            $company->contact = $request->get('contact');
            $company->contact_show = $request->get('contact_show');
            $company->telephone_show = $request->get('telephone_show');
            if (!$company->save()) {
                return json_encode(['data' => '', 'msg' => '写入失败', 'status' => 0]);
            }
            //几种情况 如果已经认证返回成功
            if ($company->audit == 1) {
                return json_encode(['data' => '', 'msg' => '成功', 'status' => 1]);
            } else {
                return json_encode(['data' => '', 'msg' => '填写成功，请等待客服人员与您联系进行企业认证', 'status' => 1]);
            }
            Session::put('member',$member);
        }
        return view('company.info.index', $data);
    }


}